package com.distribute.application.api.rest.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.application.api.utils.Arith;
import com.distribute.application.api.utils.DateUtils;
import com.distribute.application.api.utils.mobile.MobileCodeUtil;
import com.distribute.framework.model.Account;
import com.distribute.framework.model.Agent;
import com.distribute.framework.model.DirectOrder;
import com.distribute.framework.model.Feedback;
import com.distribute.framework.model.Headline;
import com.distribute.framework.model.HelpCategory;
import com.distribute.framework.model.IncomeRecord;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.MembershipRecord;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.TbMsg;
import com.distribute.framework.model.TbPartners;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
@RestController(value="api.MemberUserInfoController")
public class MemberUserInfoController  extends BaseApiController{
	/**
	 * 隐藏合伙人你功能
	 * @return
	 */
	@RequestMapping("/api/memberUser/cleanHhr.do")
	public @title("隐藏合伙人你功能") Protocol cleanHhr(
			){
		Protocol protocol = Protocol.create(true, null);
		protocol.set("clok", true);
		return protocol;
	}
	/**
	 * 会员获取会员中心数据
	 * @return
	 */
	@RequestMapping("/api/memberUser/getUserInfoData.do")
	public @title("用户获取会员中心数据") Protocol getUserInfoData(
			@title("用户编号") Long userId,
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		List<Member> members=Member.where.tTop(member.getId()).list(10000);
		Account cAccount=Account.where.member(member.getId()).get();
		 Agent agent=Agent.where.member(member.getId()).merchant(banrdId).status((short)1).get();	
		 if (agent==null) {
				protocol.set("agentType", 0);
			}else {
				protocol.set("agentType", 1);//代理商
			}
		protocol.set("dzf", DirectOrder.where.member(member.getId()).merchant(banrdId).status((short)1).count());
		protocol.set("dfh", DirectOrder.where.member(member.getId()).merchant(banrdId).status((short)2).count());
		protocol.set("dsh", DirectOrder.where.member(member.getId()).merchant(banrdId).status((short)3).count());
		protocol.set("sh", DirectOrder.where.member(member.getId()).merchant(banrdId).status((short)5).count());
		protocol.set("money", cAccount.getMoney()>0?Arith.round(cAccount.getMoney(), 2):0);
		protocol.set("userInfo", member.toJSON());
		protocol.set("count", members.size()>0?members.size():0);//粉丝数量
		return protocol;
	}
	
	/**
	 * 会员获取团队顶部数据
	 * @return
	 */
	@RequestMapping("/api/memberUser/getTeamTopData.do")
	public @title("会员获取团队顶部数据") Protocol getTeamTopData(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.set("child", Member.where.tTop(member.getId()).count()>0?Member.where.tTop(member.getId()).count():0);
		protocol.set("childChild", Member.where.tToptop(member.getId()).count()>0?Member.where.tToptop(member.getId()).count():0);
		return protocol;
	}
	/**
	 * 会员获取自己的直接粉丝列表
	 * @return
	 */
	@RequestMapping("/api/memberUser/getMyFans.do")
	public @title("用户获取自己的直接粉丝列表") Protocol getMyteams(
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(Member.where.tTop(member.getId()).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<Member>(){
			@Override
			public Map format(Member t) {
				Map json = t.toJSON();
				json.put("count", Member.where.tTop(t.getId()).count());
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员查看直接粉丝的粉丝列表
	 * @return
	 */
	@RequestMapping("/api/memberUser/getMyFansChilds.do")
	public @title("用户查看直接粉丝的粉丝列表") Protocol getMyFansChilds(
			@title("用户编号") Long userId,
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(Member.where.tTop(userId).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<Member>(){
			@Override
			public Map format(Member t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 他的直接粉丝数
	 * @return
	 */
	@RequestMapping("/api/memberUser/heFansChilds.do")
	public @title("他的直接粉丝数") Protocol heFansChilds(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.set("count", Member.where.tTop(userId).count()>0?Member.where.tTop(userId).count():0);
		return protocol;
	}
	/**
	 * 会员查看自己的收益
	 * @return
	 */
	@RequestMapping("/api/memberUser/getProfitLists.do")
	public @title("会员查看自己的收益") Protocol getProfitLists(
			@title("类型") Short type
			,@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		IncomeRecord.M where = IncomeRecord.where.beneficiary(member.getId());
		if(type!=(short)0){
			where.endStatus(type);
		}
		protocol.setPage(where.page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<IncomeRecord>(){
			@Override
			public Map format(IncomeRecord t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员获取收益切换卡上的金额数据
	 * @return
	 */
	@RequestMapping("/api/memberUser/getIncomeTopDatas.do")
	public @title("会员获取收益切换卡上的金额数据") Protocol getIncomeTopDatas(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		List<IncomeRecord> list=IncomeRecord.where.beneficiary(member.getId()).list(10000);
		double a=0.0;
		double b=0.0;
		double c=0.0;
		for (IncomeRecord incomeRecord : list) {
			a=a+incomeRecord.getIncome();
			if (incomeRecord.getEndStatus()==(short)1) {
				b=b+incomeRecord.getIncome();
			}else if (incomeRecord.getEndStatus()==(short)2) {
				c=c+incomeRecord.getIncome();
			}
		}
		protocol.set("asum", Arith.round(a, 2));
		protocol.set("bsum", Arith.round(b, 2));
		protocol.set("csum", Arith.round(c, 2));
		return protocol;
	}
	/**
	 * 会员查看自己提现记录
	 * @return
	 */
	@RequestMapping("/api/memberUser/getMoneyLists.do")
	public @title("会员查看自己提现记录") Protocol getMoneyLists(
			@title("页码") Integer pageNo
			,@title("个数") Integer pageSize
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setPage(MembershipRecord.where.cashProvider(member.getId()).page(Utils.or(pageNo,0), Utils.or(pageSize,20)), new Protocol.BeanFormat<MembershipRecord>(){
			@Override
			public Map format(MembershipRecord t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员添加微信账提现记录
	 */
		@RequestMapping("/api/memberUser/getWxMoneyRecords.do")
	public @title("会员添加微信账提现记录") Protocol getWxMoneyRecords(
			@title("formId") String formId
			,@title("金额") Double money
			,@title("规则") Short gz
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
		if (money>account.getMoney()) {
			protocol.set("msg", "提现金额不足");
			protocol.set("type", 2);
			return protocol;
		}
		if (money>5000) {
			protocol.set("msg", "每人每天提现上限5000元");
			protocol.set("type", 2);
			return protocol;
		}
		List<MembershipRecord> membershipRecords=MembershipRecord.where.cashProvider(member.getId()).auditType((short)1).list(-1);
		if (membershipRecords.size()>0) {
			protocol.set("msg", "您还有未审核通过的提现申请");
			protocol.set("type", 2);
			return protocol;
		}
		account.setMoney(Arith.sub(account.getMoney(), money));
		account.update();
		TbMsg msg2=new TbMsg(
				member.getId()//会员
				 	,"您发起了提现申请:"+money//内容
				 	,new Date()//时间
					);
   		msg2.save();	
		MembershipRecord sRecord=new MembershipRecord(
				member.getId()+"_"+new Date().getTime()
				,(short)1//1微信提现2支付宝提现
				,member.getNickName()//收款姓名 [非空]
			 	,member.getMobile()//提现电话
			 	,member.getId()//提现人
			 	,"微信提现"
			 	,money//提现金额
			 	,(short)1//审核类型
			 	,""//拒绝理由
			 	,null//条款
			 	,formId
			 	,""
			 	,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)1//状态 [非空]
				);
		sRecord.save();
		protocol.set("type", 1);
		protocol.set("msg", "提交成功！等待审核");
		return protocol;
	}
/**
 * 获取自己所有街道
 * @return
 */
@RequestMapping("/api/memberUser/getJeDaoLists.do")
public @title("获取自己所有街道") Protocol getJeDaoLists(
		){
	Protocol protocol = Protocol.create(true, null);
	Member member=Member.where.uid(this.getUid()).get();
	List<TbPartners> partners=TbPartners.where.tbpMember(member.getUid()).list(1000);
	List<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
	if (partners.size()>0) {
		for (TbPartners tbPartners : partners) {
			HashMap<String, Object> map=new HashMap<String, Object>();
			map.put("id", tbPartners.getTbpJiedao());
			map.put("name", tbPartners.get$tbpJiedao().getName());
			list.add(map);
		}
	}
	protocol.set("jd", list);
     return protocol;
}
	/**
	 * 会员添加提现记录
	 * @return
	 */
	@RequestMapping("/api/memberUser/getMoneyRecords.do")
	public @title("会员添加提现记录") Protocol getMoneyRecords(
			@title("账户编号") Long accountId
			,@title("验证码") String code
			,@title("formId") String formId
			,@title("金额") Double money
			,@title("规则") Short gz
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.id(accountId).get();
		if (account.getContactNumber()==null||account.getContactNumber().equals("")) {
			protocol.set("msg", "请设置提现账号");
			protocol.set("type", 2);
			return protocol;
		}
		if (!MobileCodeUtil.validateCaptcha(account.getContactNumber(), code)) {
			protocol.set("msg", "验证码有误");
			protocol.set("type", 2);
			return protocol;
		}
		if (money>account.getMoney()) {
			protocol.set("msg", "提现金额不足");
			protocol.set("type", 2);
			return protocol;
		}
		if (money>5000) {
			protocol.set("msg", "每人每天提现上限5000元");
			protocol.set("type", 2);
			return protocol;
		}
		List<MembershipRecord> membershipRecords=MembershipRecord.where.cashProvider(member.getId()).auditType((short)1).list(-1);
		if (membershipRecords.size()>0) {
			protocol.set("msg", "您还有未审核通过的提现申请");
			protocol.set("type", 2);
			return protocol;
		}
		account.setMoney(Arith.sub(account.getMoney(), money));
		account.update();
		TbMsg msg2=new TbMsg(
				member.getId()//会员
				 	,"您发起了提现申请:"+money//内容
				 	,new Date()//时间
					);
   		msg2.save();	
		MembershipRecord sRecord=new MembershipRecord(
				member.getId()+"_"+new Date().getTime()
				,(short)2
				,account.getNameContact()//收款姓名 [非空]
			 	,account.getContactNumber()//提现电话
			 	,member.getId()//提现人
			 	,account.getAlipayNo()
			 	,money//提现金额
			 	,(short)1//审核类型
			 	,""//拒绝理由
			 	,null//条款
			 	,formId
			 	,""
			 	,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)1//状态 [非空]
				);
		sRecord.save();
		protocol.set("type", 1);
		protocol.set("msg", "提交成功！等待审核");
		return protocol;
	}
	/**
	 * 会员获取提现账户信息
	 * @return
	 */
	@RequestMapping("/api/memberUser/memberGetsAccount.do")
	public @title("会员获取提现账户信息") Protocol memberGetsAccount(
			@title("用户编号") Long userId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
		protocol.set("account", account.toJSON());
		return protocol;
	}
	/**
	 * 会员修改提现账户信息
	 * @return
	 */
	@RequestMapping("/api/memberUser/editMemberAccount.do")
	public @title("会员修改提现账户信息") Protocol editMemberAccount(
			 @title("联系人") String name
			,@title("联系电话") String mobile
			,@title("其它账号名") String nameOther
			,@title("其它账号") String mobileOther
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Account account=Account.where.member(member.getId()).get();
		account.setNameContact(name);
		account.setContactNumber(mobile);
		account.setAlipayName(nameOther);
		account.setAlipayNo(mobileOther);
		account.update();
		protocol.set("msg", "设置成功");
		return protocol;
	}
	/**
	 * 会员提交问题反馈
	 * @return
	 */
	@RequestMapping("/api/memberUser/addFeedBack.do")
	public @title("会员提交问题反馈") Protocol addFeedBack(
			@title("问题简述") String title,
			@title("内容") String content
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Feedback feedback=new Feedback(
				title//问题简述 [非空]
			 	,member.getId()
			 	,member.getNickName()//昵称 [非空]
			 	,member.getMobile()//手机号 [非空]
			 	,content//反馈内容 [非空]
			 	,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)1//状态 [非空]
				);
		feedback.save();
		protocol.set("msg", "反馈成功");
		return protocol;
	}
	/**
	 * 根据获取品牌商帮助问题列表
	 * @return
	 */
	@RequestMapping("/api/memberUser/banrdQuestions.do")
	public @title("根据获取品牌商帮助问题列表") Protocol banrdQuestions(
			@title("品牌编号") Long banrdId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setList(HelpCategory.where.merchant(banrdId).list(-1),  new Protocol.BeanFormat<HelpCategory>(){
			@Override
			public Map format(HelpCategory t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 会员修改所在地址
	 * @return
	 */
	@RequestMapping("/api/memberUser/editMeberPlace.do")
	public @title("会员修改所在地址") Protocol editMeberPlace(
			@title("地址编号") Long adressId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		member.setPlace(adressId);
		member.update();
		protocol.set("userInfo", member.toJSON());
		return protocol;
	}
}
